//
//  PlanningDayDetailController.m
//  planningV3
//
//  Created by richard_local on 20/09/13.
//  Copyright (c) 2013 ierdre.com. All rights reserved.
//

#import "PlanningDayDetailController.h"
#import "dayDetail.h"
#import "dayAllCell.h"
#import "dayGanttViewViewController.h"
@interface PlanningDayDetailController ()

@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation PlanningDayDetailController


- (void)configureView
{
    // Update the user interface for the detail item.
    
    if (self.detailItem) {
        self.detailDescriptionLabel.text = @"TEST";
    }
}


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//section entete
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
   // [label setFont:[UIFont boldSystemFontOfSize:12]];
    NSString *string =@"Lundi 15 septembre ";
//    SERVICE(Acceuil) FORMAT-HORAIRE(jour)
    /* Section header is in 0th index... */
    [label setText:string];
    //[label setBackgroundColor:[UIColor clearColor]];
    [view addSubview:label];
    //[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    [label setBackgroundColor:[UIColor lightGrayColor]];
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0) {
        return 24;
    }
    else if(indexPath.row == 2) {
        return 24;
    }
    else {
        return 44;
    }

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 25;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 4;
}

#define DAYDETAILCELL @"dayAllCell"
#define DAYSERVICE    @"ServiceDetail"
#define IND_DAYDETAILCELL 11
#define IND_DAYSERVICE 12

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = DAYDETAILCELL;
    UITableViewCell *cell;
    if(indexPath.row == 0) {
        CellIdentifier = DAYSERVICE;
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.tag =  IND_DAYSERVICE;
        cell.backgroundColor = [UIColor lightGrayColor];
    }
    else if(indexPath.row == 2) {
        CellIdentifier = DAYSERVICE;
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.tag =  IND_DAYSERVICE;
                cell.backgroundColor = [UIColor lightGrayColor];
    }
    else {
        CellIdentifier = DAYDETAILCELL;
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.tag =  IND_DAYDETAILCELL;

    }

    //ServiceDetail
    // Configure the cell...
    // if daycell alors on charge le daydetail correspondant et on lui  file de façon à ce qui le dessine correctement 
    
    if (cell.tag ==  IND_DAYDETAILCELL) {
        dayDetail *temp = [[dayDetail alloc] init];
        dayAllCell * celltmp = (dayAllCell *) cell;
        celltmp.data = temp;
        [celltmp reset];
    }
    
    //
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}





#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
     NSLog (@" ON tourn DAy %@ %@", barButtonItem, [self.navigationController viewControllers]);
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    UIViewController * current = [[self.navigationController viewControllers] lastObject];
    [current.navigationItem setRightBarButtonItem:barButtonItem animated:YES];
    //[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    //current.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    NSLog (@" ON tournE day");
    UIViewController * current = [[self.navigationController viewControllers] lastObject];

    // Called when the view is shown again in the split view, invalidating the button and popover controller.

    [current.navigationItem setLeftBarButtonItem:nil animated:YES];
   // self.masterPopoverController = nil;
}

// segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Assume self.view is the table view
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
 //   DetailObject *detail = [self detailForIndexPath:path];
    [((dayGanttViewViewController *)segue.destinationViewController) setDetail:path.section];
}

@end
